#include <stdio.h>
#include "mazelib.h"
#include "simpio.h"

static int steps = 0;

static bool SolveMaze(pointT pt);
static bool SolveMazeUnmark(pointT pt);

int main () {
	int length;
	string maze;

	printf("-- Assignment 4B --\n\n");
	printf("Name of the maze: ");
	maze = GetLine();
	ReadMazeMap(maze);
	SetPauseTime(0.005);

	SolveMaze(GetStartPosition());
	printf("Steps without unmark: %d\n\n", steps);

	ReadMazeMap(maze);
	steps = 0;
	SolveMazeUnmark(GetStartPosition());
	printf("Steps with unmark: %d\n\n", steps);
}

static bool SolveMaze(pointT pt){
	directionT dir;
	steps++;
	if (OutsideMaze(pt)) return (TRUE);
	if (IsMarked(pt)) return (FALSE);
	MarkSquare(pt);
	for (dir = North; dir <= West; dir++)
		if ( !WallExists(pt, dir)
			&& SolveMaze(AdjacentPoint(pt, dir)) )
			return (TRUE);

	return (FALSE);
}

static bool SolveMazeUnmark(pointT pt){
	directionT dir;
	steps++;
	if (OutsideMaze(pt)) return (TRUE);
	if (IsMarked(pt)) return (FALSE);

	MarkSquare(pt);
	for (dir = North; dir <= West; dir++)
		if ( !WallExists(pt, dir)
			&& SolveMazeUnmark(AdjacentPoint(pt, dir)) )
			return (TRUE);

	UnmarkSquare(pt);
	return (FALSE);
}
